package cn.tedu.boot.ers.controller;

import cn.tedu.boot.ers.config.Constant;
import cn.tedu.boot.ers.entity.Admin;
import cn.tedu.boot.ers.entity.Category;
import cn.tedu.boot.ers.entity.User;
import cn.tedu.boot.ers.pojo.dto.AddCategoryDTO;
import cn.tedu.boot.ers.pojo.dto.UpdateCategoryDTO;
import cn.tedu.boot.ers.pojo.vo.CategoryVO;
import cn.tedu.boot.ers.service.CategoryService;
import cn.tedu.boot.ers.service.IAdminService;
import cn.tedu.boot.ers.service.IUserService;
import cn.tedu.boot.ers.web.JsonResult;
import cn.tedu.boot.ers.web.State;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description: 目录controller
 * @Author: han
 * @Date:
 **/
@Slf4j
@Api(tags = "商品目录管理模块")
@RestController
//@RequestMapping("/admin")
public class CategoryController {


    @Autowired
    IAdminService iAdminService;

    @Autowired
    CategoryService categoryService;

    /**
     * 后台添加目录
     */
    @ApiOperation("后台添加目录")
    @ApiOperationSupport(order = 100)
    @PostMapping("admins/category/add")
    @ResponseBody
    public JsonResult addCategory(HttpSession session, @Validated AddCategoryDTO addCategoryDTO) {
        Admin currentUser = (Admin) session.getAttribute(Constant.ERS_USER);
        if (currentUser == null) {
            return JsonResult.fail(State.ERR,"为空");
        }
        //校验是否是管理员
        boolean adminRole = iAdminService.checkAdminRole(currentUser);
        if (adminRole) {
            //是管理员，执行操作
            categoryService.add(addCategoryDTO);
            return JsonResult.ok();
        } else {
            return JsonResult.fail(State.ERR,"需要管理员操作");
        }
    }

    @ApiOperation("后台更新目录")
    @ApiOperationSupport(order = 101)
    @PostMapping("admins/category/update")
    @ResponseBody
    public JsonResult updateCategory(@Validated UpdateCategoryDTO updateCategoryDTO,
                                          HttpSession session) {
        Admin currentUser = (Admin) session.getAttribute(Constant.ERS_USER);
        if (currentUser == null) {
            return JsonResult.fail(State.ERR,"需要管理员登录");
        }
        //校验是否是管理员
        boolean adminRole = iAdminService.checkAdminRole(currentUser);
        if (adminRole) {
            //是管理员，执行操作
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryDTO, category);
            categoryService.update(category);
            return JsonResult.ok();
        } else {
            return JsonResult.fail(State.ERR,"需要管理员身份");
        }
    }

    @ApiOperation("后台删除目录")
    @ApiOperationSupport(order = 102)
    @PostMapping("admins/category/delete")
    @ResponseBody
    public JsonResult deleteCategory(@RequestParam Integer id) {
        categoryService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("后台目录列表")
    @ApiOperationSupport(order = 103)
    @GetMapping("admins/category/list")
    @ResponseBody
    public JsonResult listCategoryForAdmin(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return JsonResult.ok(pageInfo);
    }

    @ApiOperation("前台目录列表")
    @ApiOperationSupport(order = 104)
    @GetMapping("category/list")
    @ResponseBody
    public JsonResult listCategoryForCustomer() {
        List<CategoryVO> categoryVOS = categoryService.listCategoryForCustomer(0);
        return JsonResult.ok(categoryVOS);
    }
}
